import Toast from 'tdesign-miniprogram/toast/index';
import { fetchGood } from '../../../services/good/fetchGood';
import { fetchActivityList } from '../../../services/activity/fetchActivityList';
import { getGoodsDetailsCommentList } from '../../../services/good/fetchGoodsDetailsComments';
import { api } from '../../../config/api';
import { request } from '../../../utils/request'
import { cdnBase } from '../../../config/index';

const app = getApp();
const imgPrefix = `${cdnBase}/`;

const recLeftImg = `${imgPrefix}common/rec-left.png`;
const recRightImg = `${imgPrefix}common/rec-right.png`;
const obj2Params = (obj = {}, encode = false) => {
	const result = [];
	Object.keys(obj).forEach((key) =>
		result.push(`${key}=${encode ? encodeURIComponent(obj[key]) : obj[key]}`),
	);
	return result.join('&');
};

Page({
	data: {
		commentsList: [],
		commentsStatistics: {
			badCount: 0,
			commentCount: 0,
			goodCount: 0,
			goodRate: 0,
			hasImageCount: 0,
			middleCount: 0,
		},
		isShowPromotionPop: false,
		activityList: [],
		recLeftImg,
		recRightImg,
		details: {},
		goodsTabArray: [
			{
				name: '商品',
				value: '', // 空字符串代表置顶
			},
			{
				name: '详情',
				value: 'goods-page',
			},
		],
		storeLogo: `${imgPrefix}common/store-logo.png`,
		storeName: 'JuCheap旗舰店',
		jumpArray: [
			{
				title: '首页',
				url: '/pages/home/home',
				iconName: 'home',
			},
			{
				title: '购物车',
				url: '/pages/cart/index',
				iconName: 'cart',
				showCartNum: true,
			},
		],
		isStock: true,
		cartNum: 0,
		soldout: false,
		buttonType: 1,
		buyNum: 1,
		selectedAttrStr: '',
		skuArray: [],
		//选择的sku
		selectItem: {},
		primaryImage: '',
		specImg: '',
		isSpuSelectPopupShow: false,
		isAllSelectedSku: false,
		buyType: 0,
		outOperateStatus: false, // 是否外层加入购物车
		operateType: 0,
		selectSkuSellsPrice: 0,
		maxLinePrice: 0,
		minSalePrice: 0,
		maxSalePrice: 0,
		list: [],
		spuId: '',
		navigation: { type: 'fraction' },
		current: 0,
		autoplay: true,
		duration: 500,
		interval: 5000,
		soldNum: 0, // 已售数量
	},

	handlePopupHide() {
		this.setData({
			isSpuSelectPopupShow: false,
		});
	},

	showSkuSelectPopup(type) {
		this.setData({
			buyType: type || 0,
			outOperateStatus: type >= 1,
			isSpuSelectPopupShow: true,
		});
	},

	buyItNow() {
		this.showSkuSelectPopup(1);
	},

	toAddCart() {
		this.showSkuSelectPopup(2);
	},

	toNav(e) {
		const { url } = e.detail;
		wx.switchTab({
			url: url,
		});
	},

	showCurImg(e) {
		const { index } = e.detail;
		const { images } = this.data.details;
		wx.previewImage({
			current: images[index],
			urls: images, // 需要预览的图片http链接列表
		});
	},

	onPageScroll({ scrollTop }) {
		const goodsTab = this.selectComponent('#goodsTab');
		goodsTab && goodsTab.onScroll(scrollTop);
	},

	chooseSpecItem(e) {
		const { specList } = this.data.details;
		const { selectedSku, isAllSelectedSku } = e.detail;
		if (!isAllSelectedSku) {
			this.setData({
				selectSkuSellsPrice: 0,
			});
		}
		this.setData({
			isAllSelectedSku,
		});
		this.getSkuItem(specList, selectedSku);
	},

	getSkuItem(specList, selectedSku) {
		const { skuArray, primaryImage } = this.data;
		const selectedSkuValues = this.getSelectedSkuValues(specList, selectedSku);
		let selectedAttrStr = ` 件  `;
		selectedSkuValues.forEach((item) => {
			selectedAttrStr += `，${item.specValue}  `;
		});
		const skuItems = skuArray.filter((item) => {
			let status = true;
			(item.specInfo || []).forEach((subItem) => {
				if (!selectedSku[subItem.specId] || selectedSku[subItem.specId] !== subItem.specValueId) {
					status = false;
				}
			});
			if (status) return item;
		});
		const skuItem = skuItems && skuItems.length > 0 ? skuItems[0] : null;
		console.log('选择的商品Sku:', skuItems);
		this.selectSpecsName(selectedSkuValues.length > 0 ? selectedAttrStr : '');
		if (skuItem) {
			this.setData({
				selectItem: skuItem,
				selectSkuSellsPrice: skuItem.price || 0,
			});
		} else {
			this.setData({
				selectItem: null,
				selectSkuSellsPrice: 0,
			});
		}
		this.setData({
			specImg: skuItem && skuItem.skuImage ? skuItem.skuImage : primaryImage,
		});
	},

	// 获取已选择的sku名称
	getSelectedSkuValues(skuTree, selectedSku) {
		const normalizedTree = this.normalizeSkuTree(skuTree);
		return Object.keys(selectedSku).reduce((selectedValues, skuKeyStr) => {
			const skuValues = normalizedTree[skuKeyStr];
			const skuValueId = selectedSku[skuKeyStr];
			if (skuValueId !== '') {
				const skuValue = skuValues.filter((value) => {
					return value.specValueId === skuValueId;
				})[0];
				skuValue && selectedValues.push(skuValue);
			}
			return selectedValues;
		}, []);
	},

	normalizeSkuTree(skuTree) {
		const normalizedTree = {};
		skuTree.forEach((treeItem) => {
			normalizedTree[treeItem.specId] = treeItem.specValueList;
		});
		return normalizedTree;
	},

	selectSpecsName(selectSpecsName) {
		if (selectSpecsName) {
			this.setData({
				selectedAttrStr: selectSpecsName,
			});
		} else {
			this.setData({
				selectedAttrStr: '',
			});
		}
	},

	addCart() {
		const { isAllSelectedSku, selectItem, details: { enableSpec } } = this.data;
		if (enableSpec && !isAllSelectedSku) {
			this.showTip('请选择规格');
			return;
		}
		console.log('添加购物车数量：', this.data.buyNum)
		const postData = {
			productId: this.data.spuId,
			skuId: selectItem.skuId,
			number: this.data.buyNum
		};
		wx.showLoading();
		let me = this;
		request.post(api.cart.add, postData).then(res => {
			me.showTip('添加成功');
			wx.hideLoading();
		}).catch(err => {
			console.log('添加购物车失败:', err);
			wx.hideLoading();
			me.showTip('添加失败');
		});
	},
	showTip(message) {
		Toast({
			context: this,
			selector: '#t-toast',
			message: message,
			icon: '',
			duration: 2000,
		});
	},
	gotoBuy(type) {
		let me = this;
		const { isAllSelectedSku, buyNum, details: { enableSpec, storeId } } = me.data;
		request.get(api.user.isLogin).then(res => {
			if (enableSpec && !isAllSelectedSku) {
				me.showTip('请选择规格');
				return;
			}
			me.handlePopupHide();
			const query = {
				quantity: buyNum,
				storeId: storeId,
				spuId: me.data.spuId,
				goodsName: me.data.details.title,
				skuId: type === 1 ? me.data.skuList[0].skuId : me.data.selectItem.skuId,
				available: me.data.details.available,
				price: type === 1 ? me.data.details.minSalePrice : me.data.selectSkuSellsPrice,
				specInfo: me.data.details.specList?.map((item) => ({
					specTitle: item.title,
					specValue: item.specValueList[0].specValue,
				})),
				primaryImage: me.data.details.primaryImage,
				spuId: me.data.details.spuId,
				thumb: me.data.details.primaryImage,
				title: me.data.details.title,
			};
			let urlQueryStr = obj2Params({
				goodsRequestList: JSON.stringify([query]),
			});
			urlQueryStr = urlQueryStr ? `?${urlQueryStr}` : '';
			const path = `/pages/order/order-confirm/index${urlQueryStr}`;
			wx.navigateTo({
				url: path,
			});
		});
	},

	specsConfirm() {
		const { buyType } = this.data;
		if (buyType === 1) {
			this.gotoBuy();
		} else {
			this.addCart();
		}
		// this.handlePopupHide();
	},

	changeNum(e) {
		this.setData({
			buyNum: e.detail.buyNum,
		});
	},

	closePromotionPopup() {
		this.setData({
			isShowPromotionPop: false,
		});
	},

	promotionChange(e) {
		const { index } = e.detail;
		wx.navigateTo({
			url: `/pages/promotion-detail/index?promotion_id=${index}`,
		});
	},

	showPromotionPopup() {
		this.setData({
			isShowPromotionPop: true,
		});
	},

	getDetail(spuId) {
		Promise.all([fetchGood(spuId), fetchActivityList(1, 5, spuId)]).then((res) => {
			const [details, activityList] = res;
			const skuArray = [];
			const {
				skuList,
				primaryImage,
				isPutOnSale,
				minSalePrice,
				maxSalePrice,
				maxLinePrice,
				soldNum,
			} = details;
			skuList.forEach((item) => {
				skuArray.push({
					...item,
					quantity: item.stockInfo ? item.stockInfo.stockQuantity : 0
				});
			});
			const promotionArray = [];
			activityList.forEach((item) => {
				promotionArray.push({
					tag: item.promotionSubCode === 'MYJ' ? '满减' : '满折',
					label: item.title
				});
			});
			this.setData({
				details,
				activityList,
				isStock: details.spuStockQuantity > 0,
				maxSalePrice: maxSalePrice ? parseInt(maxSalePrice) : 0,
				maxLinePrice: maxLinePrice ? parseInt(maxLinePrice) : 0,
				minSalePrice: minSalePrice ? parseInt(minSalePrice) : 0,
				list: promotionArray,
				skuArray: skuArray,
				primaryImage,
				soldout: isPutOnSale === 0,
				soldNum,
			});
		});
	},

	async getCommentsList() {
		try {
			const data = await getGoodsDetailsCommentList(this.data.spuId);
			const homePageComments = data.pageList;
			const nextState = {
				commentsList: homePageComments.map((item) => {
					return {
						goodsSpu: item.spuId,
						userName: item.isAnonymity ? '匿名用户' : item.userName || '',
						commentScore: item.commentScore,
						commentContent: item.commentContent || '用户未填写评价',
						userHeadUrl: item.userHeadUrl
					};
				}),
			};
			this.setData(nextState);
		} catch (error) {
			console.error('comments error:', error);
		}
	},

	onShareAppMessage() {
		// 自定义的返回信息
		const { selectedAttrStr } = this.data;
		let shareSubTitle = '';
		if (selectedAttrStr.indexOf('件') > -1) {
			const count = selectedAttrStr.indexOf('件');
			shareSubTitle = selectedAttrStr.slice(count + 1, selectedAttrStr.length);
		}
		const customInfo = {
			imageUrl: this.data.details.primaryImage,
			title: this.data.details.title + shareSubTitle,
			path: `/pages/goods/details/index?spuId=${this.data.spuId}`,
		};
		return customInfo;
	},

	/** 获取评价统计 */
	async getCommentsStatistics() {
		try {
			const data = await request.get(api.product.commentStat, { spuId: this.data.spuId });
			const {
				badCount,
				commentCount,
				goodCount,
				goodRate,
				hasImageCount,
				middleCount,
			} = data;
			const nextState = {
				commentsStatistics: {
					badCount: parseInt(`${badCount}`),
					commentCount: parseInt(`${commentCount}`),
					goodCount: parseInt(`${goodCount}`),
					/** 后端返回百分比后数据但没有限制位数 */
					goodRate: Math.floor(goodRate * 10) / 10,
					hasImageCount: parseInt(`${hasImageCount}`),
					middleCount: parseInt(`${middleCount}`),
				},
			};
			this.setData(nextState);
		} catch (error) {
			console.error('comments statiistics error:', error);
		}
	},

	/** 跳转到评价列表 */
	navToCommentsListPage() {
		wx.navigateTo({
			url: `/pages/goods/comments/index?spuId=${this.data.spuId}`,
		});
	},

	onLoad(query) {
		const { spuId } = query;
		this.setData({
			spuId: spuId,
		});
		this.getDetail(spuId);
		this.getCommentsList(spuId);
		this.getCommentsStatistics(spuId);
	},
});
